/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AgendamentoConsulta.java
 *
 * Created on 16/06/2012, 16:27:49
 */
package View;

import Dao.SistemaDAO;
import Modelo.Consulta;
import Modelo.Especialidade;
import Modelo.Medico;
import Modelo.Paciente;
import Modelo.TabelaConsulta;
import Util.PAPUtil;
import com.sun.xml.internal.bind.v2.model.impl.ModelBuilder;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JFrame;
//import net.sf.nachocalendar.CalendarFactory;
//import net.sf.nachocalendar.components.DateField;

/**
 *
 * @author big-gravatai
 */
public class AgendamentoConsulta extends javax.swing.JFrame {

    //private DateField datefield;
    private PAPUtil validacoes = new PAPUtil();
    private Medico medicoGlobal = new Medico();
    //private Paciente pacienteGlobal = new Paciente();
    private SistemaDAO sisDao = new SistemaDAO();
    private LocAtmMedico telaAnterior;
    private Consulta consultaAgendar = new Consulta();
    private ArrayList<Especialidade> listaDeEspecialidades = new ArrayList<Especialidade>();
    private ArrayList<Medico> listaDeMedicos = new ArrayList<Medico>();
    private ArrayList<Consulta> listaDeconsultas = new ArrayList<Consulta>();
    private ArrayList<Date> listaDeHoras = new ArrayList<Date>();

    /** Creates new form AgendamentoConsulta */
    @SuppressWarnings("OverridableMethodCallInConstructor")
    public AgendamentoConsulta(LocAtmMedico loc) {

        initComponents();
        this.telaAnterior = loc;
        //jBtOK.setCursor(new Cursor(Cursor.HAND_CURSOR));
        //jPnCalendario.add(datefield);
       // inicializar();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jBtOK = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTxtNomePaciente = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jBtCancelar = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTxtNumeroConsulta = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jBtNovaPequisa = new javax.swing.JButton();
        jCbEspecialidade = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTbAgendamento = new javax.swing.JTable();
        jCbMedico = new javax.swing.JComboBox();
        jCbHora = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jBtExcluirConsulta = new javax.swing.JButton();
        jFTxtData = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        jPnCalendario = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMbAcoes = new javax.swing.JMenu();
        jMiConcluirAtendimento = new javax.swing.JMenuItem();
        jMiSair = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Atendimento Farmácia");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setIconImages(null);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Agendamento de Consulta"));

        jBtOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/V.png"))); // NOI18N
        jBtOK.setBorder(null);
        jBtOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtOKActionPerformed(evt);
            }
        });

        jLabel1.setText("Data:");

        jLabel3.setText("Nome Paciente:");

        jLabel4.setText("Médico:");

        jBtCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/X.png"))); // NOI18N
        jBtCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtCancelarActionPerformed(evt);
            }
        });

        jLabel5.setText("Nº Consulta:");

        jLabel6.setText("Hora:");

        jTxtNumeroConsulta.setEditable(false);

        jLabel7.setText("Especialidade:");

        jBtNovaPequisa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/bonecoPesquisaPequeno.png"))); // NOI18N
        jBtNovaPequisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtNovaPequisaActionPerformed(evt);
            }
        });

        jCbEspecialidade.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione." }));

        jTbAgendamento.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Médico", "Data", "Hora"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTbAgendamento);

        jCbMedico.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione." }));
        jCbMedico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCbMedicoActionPerformed(evt);
            }
        });

        jCbHora.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione.", "08:00:00", "08:30:00", "09:00:00", "09:30:00", "10:00:00", "10:30:00", "11:00:00", "11:30:00", "13:30:00", "14:00:00", "14:30:00", "15:00:00", "15:30:00", "16:00:00", "16:30:00", "17:00:00", "17:30:00" }));
        jCbHora.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCbHoraActionPerformed(evt);
            }
        });

        jLabel8.setText("Consultas agendadas:");

        jBtExcluirConsulta.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/ExcluirPequeno.png"))); // NOI18N

        try {
            jFTxtData.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFTxtData.setToolTipText("Formato = dd/mm/yyyy");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8)
                    .addComponent(jLabel5)
                    .addComponent(jLabel7)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTxtNumeroConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addComponent(jBtNovaPequisa, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jBtExcluirConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jBtCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jBtOK, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                        .addComponent(jTxtNomePaciente, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 440, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jFTxtData, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                                .addComponent(jCbEspecialidade, javax.swing.GroupLayout.Alignment.TRAILING, 0, 176, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addComponent(jLabel6)
                                    .addGap(18, 18, 18)
                                    .addComponent(jCbHora, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jCbMedico, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addGap(70, 70, 70))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(jTxtNumeroConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jCbEspecialidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4)
                        .addComponent(jCbMedico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel6)
                    .addComponent(jCbHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFTxtData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxtNomePaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addComponent(jLabel8)
                .addGap(6, 6, 6)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(77, 77, 77)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jBtNovaPequisa, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jBtCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jBtOK, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBtExcluirConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addGap(40, 40, 40))
        );

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/old_telephone.png"))); // NOI18N

        javax.swing.GroupLayout jPnCalendarioLayout = new javax.swing.GroupLayout(jPnCalendario);
        jPnCalendario.setLayout(jPnCalendarioLayout);
        jPnCalendarioLayout.setHorizontalGroup(
            jPnCalendarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPnCalendarioLayout.setVerticalGroup(
            jPnCalendarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(134, 134, 134)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 79, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(43, 43, 43)
                .addComponent(jPnCalendario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(200, 200, 200)
                        .addComponent(jLabel2))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(212, 212, 212)
                        .addComponent(jPnCalendario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(133, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(102, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(82, 82, 82))
        );

        jMbAcoes.setText("Ações");

        jMiConcluirAtendimento.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
        jMiConcluirAtendimento.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/VPequeno.png"))); // NOI18N
        jMiConcluirAtendimento.setText("Concluir Atendimento");
        jMiConcluirAtendimento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMiConcluirAtendimentoActionPerformed(evt);
            }
        });
        jMbAcoes.add(jMiConcluirAtendimento);

        jMiSair.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        jMiSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/XPequeno.png"))); // NOI18N
        jMiSair.setText("Sair");
        jMiSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMiSairActionPerformed(evt);
            }
        });
        jMbAcoes.add(jMiSair);

        jMenuBar1.add(jMbAcoes);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        getAccessibleContext().setAccessibleName("Agendamento de Consulta");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void carregaEspecialidade() {
        //especialidades = sisDao.listar("Modelo.Especialidade");
        listaDeEspecialidades = sisDao.listar("Modelo.Especialidade");
        for (Especialidade espec : listaDeEspecialidades) {
            jCbEspecialidade.addItem(espec);
        }
    }

    public void carregaMedico() {
        listaDeMedicos = sisDao.listar("Modelo.Medico");
        if (jCbEspecialidade.getSelectedIndex() > 0) {
            jCbEspecialidade.removeAllItems();
            Especialidade especialidadeSelecionada = (Especialidade) jCbEspecialidade.getSelectedItem();
            for(Medico med : listaDeMedicos){
                if (med.getEspecialidadeIdespecialidade() == especialidadeSelecionada.getIdespecialidade()){
                    jCbMedico.addItem(med);
                }
            }
        } else {
            for (Medico med : listaDeMedicos) {
                jCbMedico.addItem(med);
            }
        }
    }

    /*
     * Método carrega hora serve para carregar o jCbHora
     * com objetos do tipo Date tanto para visualização 
     * como para facilitar a persistência no banco uma 
     * vez que o objeto a ser persistido deve ser do tipo Date.
     */
    public void carregaHora() {
        if (listaDeHoras.isEmpty()) {
            listaDeHoras.add(validacoes.toBancoHora("08:30:00"));
            listaDeHoras.add(validacoes.toBancoHora("09:00:00"));
            listaDeHoras.add(validacoes.toBancoHora("09:30:00"));
            listaDeHoras.add(validacoes.toBancoHora("10:00:00"));
            listaDeHoras.add(validacoes.toBancoHora("10:30:00"));
            listaDeHoras.add(validacoes.toBancoHora("11:00:00"));
            listaDeHoras.add(validacoes.toBancoHora("11:30:00"));
            listaDeHoras.add(validacoes.toBancoHora("13:30:00"));
            listaDeHoras.add(validacoes.toBancoHora("14:00:00"));
            listaDeHoras.add(validacoes.toBancoHora("14:30:00"));
            listaDeHoras.add(validacoes.toBancoHora("15:00:00"));
            listaDeHoras.add(validacoes.toBancoHora("15:30:00"));
            listaDeHoras.add(validacoes.toBancoHora("16:00:00"));
            listaDeHoras.add(validacoes.toBancoHora("16:30:00"));
            listaDeHoras.add(validacoes.toBancoHora("17:00:00"));
            listaDeHoras.add(validacoes.toBancoHora("17:30:00"));
        } else {
            jCbHora.removeAllItems();
            Medico medicoselecionado = new Medico();
            medicoselecionado = (Medico) jCbMedico.getSelectedItem();
            ArrayList<Consulta> consultasAgendadasMedico = new ArrayList<Consulta>();

            consultasAgendadasMedico = (ArrayList<Consulta>) sisDao.listaCondicao("Modelo.Consulta",
                    String.valueOf(medicoselecionado.getIdmedico()), null, null, "idmedico", 3);
            for (Date horaConsulta : listaDeHoras) {// Percorre Array de horários
                int indiceListaDeHoras = 0;
                for (Consulta c : consultasAgendadasMedico) {// Percorre Array de consultas do médico selecionado.
                    if (!c.getStatusConsulta()) { // Verifica se a consulta já foi atendida\ concluida.
                        String hora = validacoes.toHoraNormal(c.getHora());
                        String horaObjetoLista = validacoes.toHoraNormal(listaDeHoras.get(indiceListaDeHoras));
                        if (!hora.equals(horaObjetoLista)) {
                            /* Inclui no jCbHora se hora da consulta agendada
                             * do médico diferente de hora do Array de horários
                             * de expediente, indica que horário vago.
                             */
                            jCbHora.addItem(listaDeHoras.get(indiceListaDeHoras));
                            break;
                        }
                    }
                    indiceListaDeHoras++;
                }
            }
        }

    }

   /* public void inicializar() {
        this.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH); //Maximiza a tela
        jBtCancelar.setToolTipText("Cancelar");
        jBtNovaPequisa.setToolTipText("Nova Pesquisa");
        jBtOK.setToolTipText("OK");
        jBtExcluirConsulta.setToolTipText("Excluir");
        //jTbAgendamento.setModel(new TabelaConsulta());
        carregaEspecialidade();
        carregaMedico();
        carregaHora();
        if (!telaAnterior.novo) {
            preencherDadosConsultaAgendada();
        } else {
            preencherDadosNovaConsulta();
        }
    }

    public void preencherDadosNovaConsulta() {
        habilitaEdicaoDeDados(true);
    }

    public void preencherDadosConsultaAgendada() {

        listaDeconsultas = (ArrayList<Consulta>) sisDao.listaCondicao("Modelo.Consulta",
                String.valueOf(telaAnterior.pacienteGlobal.getIdpaciente()), null, null, "pacienteIdpaciente", 3);
        consultaAgendar = listaDeconsultas.get(0);
        //TO DO pegar indice da tabela para setar consultaAgendar;
        ArrayList<Medico> listatemp = new ArrayList<Medico>();
        Medico medTemp = new Medico();

        for (Consulta c : listaDeconsultas) {
            // TO DO evitar repetição de médicos na combo;
            listatemp = (ArrayList<Medico>) sisDao.listaCondicao("Modelo.Medico",
                    String.valueOf(c.getMedicoIdmedico()), null, null, "idmedico", 3);

            if (listatemp != null) {
                medTemp = listatemp.get(0);
                if (c.getMedicoIdmedico() == consultaAgendar.getMedicoIdmedico()) {
                    medicoGlobal = medTemp;
                }
            } else {
                break;
            }
        }

        jCbMedico.setSelectedItem(medicoGlobal);
        for (Especialidade espec : listaDeEspecialidades) {
            if (espec.getIdespecialidade() == medicoGlobal.getEspecialidadeIdespecialidade()) {
                jCbEspecialidade.setSelectedItem(espec);
            }
        }
        String x = validacoes.toDataNormal(consultaAgendar.getDataConsulta());
        jFTxtData.setText(x);
        jTxtNomePaciente.setText(telaAnterior.pacienteGlobal.getNome());
        //carregaHora();
        // TO DO preencher tabela com consultas agendadas para o paciente.
    }
*/
    private void jBtOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtOKActionPerformed
        AgendarConsulta();

    }//GEN-LAST:event_jBtOKActionPerformed

    private void jMiSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMiSairActionPerformed
        this.dispose();
    }//GEN-LAST:event_jMiSairActionPerformed

    private void jMiConcluirAtendimentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMiConcluirAtendimentoActionPerformed
        concluirAtendimento();
    }//GEN-LAST:event_jMiConcluirAtendimentoActionPerformed

    private void jBtCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtCancelarActionPerformed
        limparTela();
    }//GEN-LAST:event_jBtCancelarActionPerformed

    private void jBtNovaPequisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtNovaPequisaActionPerformed
        //abrir nova pesquisa de Receita;
    }//GEN-LAST:event_jBtNovaPequisaActionPerformed

    private void jCbMedicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCbMedicoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCbMedicoActionPerformed

    private void jCbHoraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCbHoraActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCbHoraActionPerformed

    public void concluirAtendimento() {
        //Gravar algum registro que a receita foi vendida.
        new Mensagem(this, "Venda realizada com sucesso!", "OK");
        limparTela();
    }

    public void AgendarConsulta() {
        if (jCbEspecialidade.getSelectedIndex() == 0) {
            new Mensagem(this, "Selecione uma especialidade!", null);
            jCbEspecialidade.requestFocus();
            return;
        }

        if (jCbMedico.getSelectedIndex() != 0) {
            Medico med = new Medico();
            med = (Medico) jCbMedico.getSelectedItem();
            consultaAgendar.setMedicoIdmedico(med.getIdmedico());
        } else {
            new Mensagem(this, "Selecione um médico para o atendimento!", null);
            jCbMedico.requestFocus();
            return;
        }

        Date x = validacoes.toBancoData(jFTxtData.getText());
        consultaAgendar.setDataConsulta(validacoes.toBancoData(jFTxtData.getText()));
        /*if (validacoes.validaData(jFTxtData.getText())) {
        Date x = validacoes.toBancoData(jFTxtData.getText());
        consultaAgendar.setDataConsulta(validacoes.toBancoData(jFTxtData.getText()));
        } else {
        new Mensagem(this, "Data inválida!/nInsira a data novamente!", null);
        jFTxtData.requestFocus();
        }*/

    /*    consultaAgendar.setHora(validacoes.toBancoHora((String) jCbHora.getSelectedItem()));
        consultaAgendar.setPacienteIdpaciente(telaAnterior.pacienteGlobal.getIdpaciente());
        consultaAgendar.setStatusConsulta(false);
        sisDao.addUpObjeto(consultaAgendar);*/
    }

    public void limparTela() {
        //jTxtCRM.setText("");
        jTxtNumeroConsulta.setText("");
        jTxtNomePaciente.setText("");
        jFTxtData.setText("");
    }

    public void habilitaEdicaoDeDados(boolean habilita) {
        jTxtNomePaciente.setEnabled(habilita);
        jFTxtData.setEnabled(habilita);
        jCbEspecialidade.setEnabled(habilita);
        jCbHora.setEnabled(habilita);
        jCbMedico.setEnabled(habilita);
        jFTxtData.setEnabled(habilita);
    }

    public void preenchertabela(ArrayList<Consulta> consultas) {
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtCancelar;
    private javax.swing.JButton jBtExcluirConsulta;
    private javax.swing.JButton jBtNovaPequisa;
    private javax.swing.JButton jBtOK;
    private javax.swing.JComboBox jCbEspecialidade;
    private javax.swing.JComboBox jCbHora;
    private javax.swing.JComboBox jCbMedico;
    private javax.swing.JFormattedTextField jFTxtData;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JMenu jMbAcoes;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMiConcluirAtendimento;
    private javax.swing.JMenuItem jMiSair;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPnCalendario;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTbAgendamento;
    private javax.swing.JTextField jTxtNomePaciente;
    private javax.swing.JTextField jTxtNumeroConsulta;
    // End of variables declaration//GEN-END:variables
}
